//
//  JCAddressPickerView.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/4/3.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCAddressPickerView.h"

@interface JCAddressPickerView()
//@property (nonatomic, strong) NSDictionary *pickerDic;
//@property (nonatomic, strong) NSArray *selectedArray;
//
///** 省份数组 */
//@property (nonatomic, strong) NSArray *provinceArray;
///** 城市数组 */
//@property (nonatomic, strong) NSArray *cityArray;
/** 省 */
@property (nonatomic,copy) NSString *province;
/** 市 */
@property (nonatomic,copy) NSString *city;
@property (nonatomic,strong)NSArray *citiesArray;



/**
 *  省模型
 */
@property (nonatomic,strong)city * selecletPro;

@property (nonatomic, strong) NSString * strModel;



@end

@implementation JCAddressPickerView


+(instancetype)showWithDataArray:(NSArray *)array block:(AddressBlcok)block {

    JCAddressPickerView *pickerView = [[JCAddressPickerView alloc] init];
    [pickerView setCitiesArray:array];
    pickerView.addressBlock = block;

    [pickerView show];
    return pickerView;

}

- (void)setupPickerView {
    [super setupPickerView];
    [self loadAddressData];
    
    
}
- (void)loadAddressData {

    self.citiesArray = [NSArray array];

}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if (component == 0) {
        
        return self.citiesArray.count;
    }else{
        NSInteger seleProIndx = [pickerView selectedRowInComponent:0];
        city * selePro = self.citiesArray[seleProIndx];
        self.selecletPro = selePro;
        return selePro.cities.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
 
    if (component == 0) {
        city * city = self.citiesArray[row];
        return city.name;
    }else{
        return self.selecletPro.cities[row][@"value"];
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.frame.size.width/2;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];

    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark - 点击确定按钮
- (void)comfirmBtnClick {
    NSInteger selectProvince = [self.pickerView selectedRowInComponent:0];
    NSInteger selectCity     = [self.pickerView selectedRowInComponent:1];
    
    city * pro = self.citiesArray[selectProvince];
    
    NSString * city = self.selecletPro.cities[selectCity][@"value"];
    
    if (self.addressBlock) {
        self.addressBlock(pro.name,city);
    }
    [super comfirmBtnClick];
}


- (void)setCitiesArray:(NSArray *)citiesArray{
    _citiesArray = citiesArray;
    [self.pickerView reloadAllComponents];

}

@end
